/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bungabungaball.gamestates;

import bungabungaball.GlobalSettings;
import bungabungaball.gamestates.menu.MenuEntry;
import bungabungaball.BungaBungaBall;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.MouseOverArea;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

/**
 * Configure your Game. For example the time or the map you are playing on.
 *
 * @author Ingo Klose, Stephan Meyer, Sebastian Lehmann
 * @version 1.0
 */
public class SpecificGame extends BasicGameState {

    /**
     *
     * The current mouse position x
     */
    private int mouseX;
    /**
     * *
     * the current mouse position y
     */
    private int mouseY;
    /**
     * The stateID
     */
    int stateID = 1;
    /**
     * MouseOverArea
     */
    private MouseOverArea startButton;
    /**
     * MenuEntry for time
     */
    private MenuEntry oneMinute;
    /**
     * MenuEntry for time
     */
    private MenuEntry twoMinutes;
    /**
     * MenuEntry for time
     */
    private MenuEntry threeMinutes;
    /**
     * MenuEntry for time
     */
    private MenuEntry fiveMinutes;
    /**
     * MenuEntry for sound
     */
    private MenuEntry soundAn;
    /**
     * MenuEntry for sound
     */
    private MenuEntry soundAus;
    /**
     * MenuEntry for disco
     */
    private MenuEntry discoAn;
    /**
     * MenuEntry for disco
     */
    private MenuEntry discoAus;
    /**
     * MenuEntry for skill CPU
     */
    private MenuEntry leicht;
    /**
     * MenuEntry for skill CPU
     */
    private MenuEntry mittel;
    /**
     * MenuEntry for skill CPU
     */
    private MenuEntry bungameister;
    /**
     * Array with saved maps
     */
    private File files[];
    /**
     * Array with saved maps
     */
    private MenuEntry MapEntries[];
    /**
     * Saves the properties for running time
     */
    private boolean[] timeCheck = {false, true, false, false};
    /**
     * Saves the properties for discoMode
     */
    private boolean[] discoCheck = {false, true};
    /**
     * Saves the properties for sound
     */
    private boolean[] soundCheck = {true, false};
    /**
     * Saves the properties for level
     */
    private boolean[] levelCheck = new boolean[30];
    /**
     * Saves the skilllevel for CPU
     */
    private boolean[] skillCheck = {false, true, false};
    /**
     * An image of the driver
     */
    private Image driver;
    /**
     * An image of the driver
     */
    private Image driver2;
    /**
     * Bunga Logo
     */
    private Image bunga;
    /**
     * Bunga Logo
     */
    private Image bungamenu;
    /**
     * x Position of the driver
     */
    private double driverX;
    /**
     * Y Position of the driver
     */
    private double driverY;
    /**
     * A counter for the driver
     */
    private double counter = 0;
    /**
     * A checker for the driver
     */
    private boolean checker = true;

    /**
     * Constructor
     *
     * @param stateID
     */
    public SpecificGame(int stateID) {
        this.stateID = stateID;
        driverX = 600;
        driverY = 350;

    }

    /**
     * Get stateID
     *
     * @return Returns stateID
     */
    @Override
    public int getID() {
        return stateID;
    }

    /**
     * Initialize the specific game
     *
     * @param gc
     * @param sbg
     * @throws SlickException
     */
    @Override
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {

        startButton = new MouseOverArea(gc, new Image("\\img\\button_letsbunga.jpg"), 700, 600);
        startButton.setMouseOverImage(new Image("\\img\\button_letsbunga2.jpg"));
        startButton.setNormalColor(Color.white);
        startButton.setMouseDownImage(new Image("\\img\\button_letsbunga.jpg"));

        oneMinute = new MenuEntry("1 Minute", 530, 150, gc.getDefaultFont());
        twoMinutes = new MenuEntry("2 Minuten", 530, 180, gc.getDefaultFont());
        threeMinutes = new MenuEntry("3 Minuten", 530, 210, gc.getDefaultFont());
        fiveMinutes = new MenuEntry("5 Minuten", 530, 240, gc.getDefaultFont());

        soundAn = new MenuEntry("An", 530, 340, gc.getDefaultFont());
        soundAus = new MenuEntry("Aus", 530, 370, gc.getDefaultFont());

        discoAn = new MenuEntry("An", 530, 450, gc.getDefaultFont());
        discoAus = new MenuEntry("Aus", 530, 480, gc.getDefaultFont());

        leicht = new MenuEntry("Leicht", 700, 150, gc.getDefaultFont());
        mittel = new MenuEntry("Normal", 700, 180, gc.getDefaultFont());
        bungameister = new MenuEntry("Bungameister", 700, 210, gc.getDefaultFont());

        MapEntries = new MenuEntry[30];

        this.readOutFiles(gc);

        driver = new Image("\\img\\player1.png");

        driver2 = new Image("\\img\\player2.png");
        driver2.setRotation(45);

        bunga = new Image("\\img\\bungabunga.png");
    }

    /**
     * Render the specific game
     *
     * @param gc
     * @param sbg
     * @param g
     * @throws SlickException
     */
    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        g.setFont(MainMenu.font);

        g.setColor(Color.black);
        g.fillRect(0, 0, 1024, 768);
        g.setColor(Color.lightGray);
        g.fillRect(650, 580, 300, 80);
        g.setColor(Color.white);
        g.drawRect(650, 580, 300, 80);
        g.drawImage(driver, (int) driverX, (int) driverY);
        g.drawImage(driver2, 230,100);
        g.drawImage(bunga, 400, 0);
        g.drawString("Map laden", 100, 100);
        //g.drawString(GlobalSettings.currentGameMode.toString(), 100, 150);
        g.drawString("Spieldauer", 530, 100);
        startButton.render(gc, g);


        if (files != null) { // Erforderliche Berechtigungen etc. sind vorhanden

            for (int i = 0; i < files.length; i++) {
                if (levelCheck[i] == true) {
                    g.setColor(Color.red);
                } else if (MapEntries[i].checkMouseIn(mouseX, mouseY)) {
                    g.setColor(Color.green);
                } else {
                    g.setColor(Color.white);
                }
                g.drawString(MapEntries[i].text, MapEntries[i].x, MapEntries[i].y);
            }
        } else {
            g.drawString("Keine Maps vorhanden", 100, 150);
        }


        if (timeCheck[0] == true) {
            g.setColor(Color.red);
        } else if (oneMinute.checkMouseIn(mouseX, mouseY)) {
            g.setColor(Color.green);
        } else {
            g.setColor(Color.white);
        }
        g.drawString(oneMinute.text, oneMinute.x, oneMinute.y);

        if (timeCheck[1] == true) {
            g.setColor(Color.red);
        } else if (twoMinutes.checkMouseIn(mouseX, mouseY)) {
            g.setColor(Color.green);
        } else {
            g.setColor(Color.white);
        }
        g.drawString(twoMinutes.text, twoMinutes.x, twoMinutes.y);

        if (timeCheck[2] == true) {
            g.setColor(Color.red);
        } else if (threeMinutes.checkMouseIn(mouseX, mouseY)) {
            g.setColor(Color.green);
        } else {
            g.setColor(Color.white);
        }
        g.drawString(threeMinutes.text, threeMinutes.x, threeMinutes.y);

        if (timeCheck[3] == true) {
            g.setColor(Color.red);
        } else if (fiveMinutes.checkMouseIn(mouseX, mouseY)) {
            g.setColor(Color.green);
        } else {
            g.setColor(Color.white);
        }
        g.drawString(fiveMinutes.text, fiveMinutes.x, fiveMinutes.y);

        g.setColor(Color.white);
        g.drawString("Sound", 530, 290);
        g.drawString("Disco-Modus", 530, 420);

        if (soundCheck[0] == true) {
            g.setColor(Color.red);
        } else if (soundAn.checkMouseIn(mouseX, mouseY)) {
            g.setColor(Color.green);
        } else {
            g.setColor(Color.white);
        }
        g.drawString(soundAn.text, soundAn.x, soundAn.y);

        if (soundCheck[1] == true) {
            g.setColor(Color.red);
        } else if (soundAus.checkMouseIn(mouseX, mouseY)) {
            g.setColor(Color.green);
        } else {
            g.setColor(Color.white);
        }
        g.drawString(soundAus.text, soundAus.x, soundAus.y);

        if (discoCheck[0] == true) {
            g.setColor(Color.red);
        } else if (discoAn.checkMouseIn(mouseX, mouseY)) {
            g.setColor(Color.green);
        } else {
            g.setColor(Color.white);
        }
        g.drawString(discoAn.text, discoAn.x, discoAn.y);

        if (discoCheck[1] == true) {
            g.setColor(Color.red);
        } else if (discoAus.checkMouseIn(mouseX, mouseY)) {
            g.setColor(Color.green);
        } else {
            g.setColor(Color.white);
        }
        g.drawString(discoAus.text, discoAus.x, discoAus.y);

        if (discoCheck[0] == true) {
            g.setColor(Color.yellow);
            g.drawString("Achtung Festhalten!!!", 570, 450);
        }

        if (GlobalSettings.currentGameMode == GlobalSettings.gameModes.SinglePlayer) {
            g.setColor(Color.white);
            g.drawString("Schwierigkeit", 700, 100);

            if (skillCheck[0] == true) {
                g.setColor(Color.red);
            } else if (leicht.checkMouseIn(mouseX, mouseY)) {
                g.setColor(Color.green);
            } else {
                g.setColor(Color.white);
            }
            g.drawString(leicht.text, leicht.x, leicht.y);

            if (skillCheck[1] == true) {
                g.setColor(Color.red);
            } else if (mittel.checkMouseIn(mouseX, mouseY)) {
                g.setColor(Color.green);
            } else {
                g.setColor(Color.white);
            }
            g.drawString(mittel.text, mittel.x, mittel.y);

            if (skillCheck[2] == true) {
                g.setColor(Color.red);
            } else if (bungameister.checkMouseIn(mouseX, mouseY)) {
                g.setColor(Color.green);
            } else {
                g.setColor(Color.white);
            }
            g.drawString(bungameister.text, bungameister.x, bungameister.y);
        }
    }

    /**
     * Update the specific game
     *
     * @param gc
     * @param sbg
     * @param i
     * @throws SlickException
     */
    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int i) throws SlickException {
        Input input = gc.getInput();

        this.readOutFiles(gc);
        if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
            if (startButton.isMouseOver()) {
                try {
                    bungabungaball.BungaBungaBall.gameplay.loadingMap(new File("maps\\" + this.actualFile()));
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(SpecificGame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(SpecificGame.class.getName()).log(Level.SEVERE, null, ex);
                }
                //bungabungaball.BungaBungaBall.gameplay.createHindernis();

                if (timeCheck[0] == true) {
                    GlobalSettings.gameRunningTime = 60000;
                }
                if (timeCheck[1] == true) {
                    GlobalSettings.gameRunningTime = 120000;
                }
                if (timeCheck[2] == true) {
                    GlobalSettings.gameRunningTime = 180000;
                }
                if (timeCheck[3] == true) {
                    GlobalSettings.gameRunningTime = 300000;
                }
                if (skillCheck[0] == true) {
                    GlobalSettings.skill = 3;
                }
                if (skillCheck[1] == true) {
                    GlobalSettings.skill = 5;
                }
                if (skillCheck[2] == true) {
                    GlobalSettings.skill = 7;
                }
                if (soundCheck[0] == true) {
                    GlobalSettings.sound = true;
                }
                if (soundCheck[1] == true) {
                    GlobalSettings.sound = false;
                }
                if (discoCheck[0] == true) {
                    GlobalSettings.discoMode = true;
                }
                if (soundCheck[1] == true) {
                    GlobalSettings.discoMode = false;
                }
                sbg.enterState(bungabungaball.BungaBungaBall.GAMEPLAYSTATESingle);
            }
            if (oneMinute.checkMouseIn(mouseX, mouseY)) {
                timeCheck[0] = true;
                timeCheck[1] = false;
                timeCheck[2] = false;
                timeCheck[3] = false;
            }
            if (twoMinutes.checkMouseIn(mouseX, mouseY)) {
                timeCheck[0] = false;
                timeCheck[1] = true;
                timeCheck[2] = false;
                timeCheck[3] = false;
            }
            if (threeMinutes.checkMouseIn(mouseX, mouseY)) {
                timeCheck[0] = false;
                timeCheck[1] = false;
                timeCheck[2] = true;
                timeCheck[3] = false;
            }
            if (fiveMinutes.checkMouseIn(mouseX, mouseY)) {
                timeCheck[0] = false;
                timeCheck[1] = false;
                timeCheck[2] = false;
                timeCheck[3] = true;
            }
            if (soundAn.checkMouseIn(mouseX, mouseY)) {
                soundCheck[0] = true;
                soundCheck[1] = false;
            }
            if (soundAus.checkMouseIn(mouseX, mouseY)) {
                soundCheck[0] = false;
                soundCheck[1] = true;
            }
            if (discoAn.checkMouseIn(mouseX, mouseY)) {
                discoCheck[0] = true;
                discoCheck[1] = false;
            }
            if (discoAus.checkMouseIn(mouseX, mouseY)) {
                discoCheck[0] = false;
                discoCheck[1] = true;
            }

            int j = 0;
            while (MapEntries[j] != null) {
                if (MapEntries[j].checkMouseIn(mouseX, mouseY)) {

                    for (int l = 0; l < levelCheck.length; l++) {
                        levelCheck[l] = false;
                    }
                    levelCheck[j] = true;
                }
                j++;
            }


            if (leicht.checkMouseIn(mouseX, mouseY)) {
                skillCheck[0] = true;
                skillCheck[1] = false;
                skillCheck[2] = false;
            }
            if (mittel.checkMouseIn(mouseX, mouseY)) {
                skillCheck[0] = false;
                skillCheck[1] = true;
                skillCheck[2] = false;
            }
            if (bungameister.checkMouseIn(mouseX, mouseY)) {
                skillCheck[0] = false;
                skillCheck[1] = false;
                skillCheck[2] = true;
            }
        }


        if (input.isKeyDown(Input.KEY_ESCAPE)) {
            try {
                Thread.sleep(250);
            } catch (InterruptedException ex) {
                Logger.getLogger(GamePlay.class.getName()).log(Level.SEVERE, null, ex);
            }
            sbg.enterState(bungabungaball.BungaBungaBall.MAINMENUSTATE);
        }

        mouseX = input.getAbsoluteMouseX();
        mouseY = input.getAbsoluteMouseY();

        this.drive();
        this.drive();
    }

    /**
     * Load the name of the actual map
     *
     * @return Mapname
     */
    public String actualFile() {
        for (int i = 0; i < levelCheck.length; i++) {
            if (levelCheck[i] == true) {
                return MapEntries[i].text;
            }
        }
        return "defaultGamePlay.txt";
    }

    /**
     * Read out the maps
     *
     * @param gc
     */
    public void readOutFiles(GameContainer gc) {

        File f = new File("maps\\");
        files = f.listFiles();

        int y = 150;
        if (files != null) { // Erforderliche Berechtigungen etc. sind vorhanden

            for (int i = 0; i < files.length; i++) {
                MapEntries[i] = new MenuEntry(files[i].getName(), 100, y, gc.getDefaultFont());
                //levelCheck[i] = false;
                y += 30;
                if (files[i].getName().equals("defaultGamePlay.txt")) {

                    MapEntries[i].text = files[i].getName();
                    //levelCheck[i] = true;
                }


            }
        }
    }

    /**
     * Updates the driver Image
     */
    public void drive() {
        //x = 600 y = 400
        double speedRotation = Math.toRadians(driver.getRotation() - 90.f);

        driverX = driverX + 2 * Math.cos(speedRotation);
        driverY = driverY + 2 * Math.sin(speedRotation);

        if (checker && counter <= 270) {
            driver.rotate(1);
        } else if (!checker && counter <= 270) {
            driver.rotate(-1);
        }
        counter++;
        if (counter == 385) {
            counter = 0;
            if (checker) {
                checker = false;
            } else {
                checker = true;
            }
        }
    }
}
